package com.bestv.search.common.service.impl;

import com.bestv.search.common.dao.ManualBindDao;
import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.Info;
import com.bestv.search.common.model.ManualBind;
import com.bestv.search.common.service.InfoManager;
import com.bestv.search.common.service.ManualBindManager;
import com.bestv.search.common.util.DateUtil;

import java.util.*;

public class ManualBindManagerImpl implements ManualBindManager {

    protected ManualBindDao manualBindDao;

    public ManualBindDao getManualBindDao() {
        return manualBindDao;
    }

    public void setManualBindDao(ManualBindDao manualBindDao) {
        this.manualBindDao = manualBindDao;
    }

    @Override
    public ManualBind getManualBindById(DataSourceKey key, Long id) {
        return manualBindDao.get(id);
    }

    @Override
    public List<ManualBind> getManualBinds(DataSourceKey key, int start, int limit) {
        if (start == -1 || limit == -1)
            return manualBindDao.getAll();
        return manualBindDao.getManualBindsPaging(start, limit);
    }

    @Override
    public ManualBind saveManualBind(DataSourceKey key, ManualBind manualBind) {
        return manualBindDao.save(manualBind);
    }

    @Override
    public ManualBind getManualBindByName(DataSourceKey key, String name) {
        return manualBindDao.getManualBindByName(name);
    }

    @Override
    public void deleteManualBindById(DataSourceKey key, Long id) {
        manualBindDao.remove(id);
    }

    @Override
    public int getInfoCount(DataSourceKey key, Long id) {
        // TODO Auto-generated method stub
        ManualBind mb = manualBindDao.get(id);
        if (mb == null)
            return 0;
        return mb.getInfos().size();
    }

    @Override
    public int getManualBindCount(DataSourceKey key) {
        return manualBindDao.getManualBindCount();
    }

    @Override
    public void saveManualBindAll(DataSourceKey key, ManualBind manualBind, List<String> infoCodes,
                                  InfoManager infoManager) {
        Date date = new Date();
        manualBindDao.save(manualBind);// 在HDQ中保存皮
        Set<Info> infos = infoManager.getInfoSetByCodes(key, infoCodes);
        if (infos != null) {
            for (Info info : infos) {
                info.setUpdateFlag(1);
                info.setUpdateTimeForIndex(date);
                info.setUpdateTimeMillis(Long.valueOf(DateUtil.convertDateToString("yyyyMMddHHmm", date)));
            }
            manualBind.setInfos(infos);
        }
    }

    @Override
    public List<ManualBind> getOwns(DataSourceKey key, int start, int limit) {
        return manualBindDao.getOwns(start, limit);
    }

    @Override
    // This method is un-used since the business logic changed for manualBind
    public Set<ManualBind> getListIncludeFailedMbs(DataSourceKey key, int start, int limit, String siteCode) {
        int firstCount = getFailedCount(key, siteCode);
        int limit_2 = limit;
        int start_2 = 0;
        if (start < firstCount) {
            limit_2 = limit - (firstCount - start);
        } else {
            start_2 = start - firstCount;
        }
        List<ManualBind> failedList = manualBindDao.getFailedList(siteCode, start, limit);
        if (failedList.size() == limit)
            return new HashSet<ManualBind>(failedList);
        List<ManualBind> siteList = manualBindDao.getManualBindsPaging(start_2, limit_2);
        Set<ManualBind> mdSet = new LinkedHashSet<ManualBind>();
        mdSet.addAll(failedList);
        mdSet.addAll(siteList);
        return mdSet;
    }

    @Override
    public int getOwnsCount(DataSourceKey key) {
        return manualBindDao.getOwnsCount();
    }

    @Override
    public int getListIncludeFailedMbsCount(DataSourceKey key) {
        return getManualBindCount(key);
    }

    @Override
    public int getFailedCount(DataSourceKey key, String siteCode) {
        return manualBindDao.getFailedCount(siteCode);
    }

    @Override
    public void deleteManualBindAll(DataSourceKey key, ManualBind mb) {
        Date date = new Date();
        Set<Info> infos = manualBindDao.get(mb.getId()).getInfos();
        if (infos != null && infos.size() > 0) {
            for (Info info : infos) {
                info.setUpdateFlag(1);
                info.setUpdateTimeForIndex(date);
                info.setUpdateTimeMillis(Long.valueOf(DateUtil.convertDateToString("yyyyMMddHHmm", date)));
            }
        }
        manualBindDao.remove(mb.getId());
    }
}
